import _ from "lodash";
// 获取后代-childeren方式
export function getOffspringnTree(aData, pid) {
  var arr = [];

  for (var i = 0, lenI = aData.length; i < lenI; i++) {
    if (aData[i].parentId == pid) {
      let aFirst = aData[i];
      if (hasOffspringn(aData, aData[i].id)) {
        let a = getOffspringnTree(aData, aData[i].id) || [];
        if (a.length) {
          aFirst.children = a;
        }
      }
      arr.push(aFirst);
    }
  }
  return arr;
}
// 获取后代-平铺方式
export function getOffspringn(aData, id, arrOld) {
  var arr = arrOld || [];

  for (var i = 0, lenI = aData.length; i < lenI; i++) {
    if (aData[i].parentId == id) {
      arr.push(aData[i]);
      if (hasOffspringn(aData, aData[i].id)) {
        getOffspringn(aData, aData[i].id, arr);
      }
    }
  }
  // console.log('etOffspringn(aData, aData[i].id)', arr)
  return arr;
}

// 获取父级-平铺方式
export function getAncestry(aData, pid, arrOld) {
  var arr = arrOld || [];

  for (var i = 0, lenI = aData.length; i < lenI; i++) {
    if (aData[i].id == pid) {
      arr.unshift(aData[i]);
      if (hasAncestry(aData, aData[i].parentId)) {
        getAncestry(aData, aData[i].parentId, arr);
      }
    }
  }
  return arr;
}

// 判断是否有子级
export function hasOffspringn(aData, id) {
  // console.log('aData', aData)
  for (var i = 0, lenI = aData.length; i < lenI; i++) {
    // console.log('hasOffspringn1', aData[i], id)
    if (aData[i].parentId == id) {
      return true;
    }
  }
  return false;
}
// 判断是否有父级
export function hasAncestry(aData, pid) {
  // console.log('aData', aData)
  for (var i = 0, lenI = aData.length; i < lenI; i++) {
    // console.log('hasOffspringn1', aData[i], id)
    if (aData[i].id == pid) {
      return true;
    }
  }
  return false;
}

/**
 * 筛选找回需要的数据
 * @param  {Array Number} arr     要转换为字符串的数组
 * @param  {Array Object} arrList 要筛选返回的，总数组
 * @return {[type]}         [description]
 */
export function selectData(arr, arrList) {
  var arrName = [];

  if (_.isString(arr) || _.isNumber(arr)) {
    for (let k = arrList.length; k--; ) {
      if (arr == arrList[k].id) {
        arrName.push(arrList[k]);
        break;
      }
    }
  } else if (_.isArray(arr)) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] == "0") {
        arrName = [{ name: "全部", id: 0 }];
        break;
      } else {
        for (let k = arrList.length; k--; ) {
          if (arr[i] == arrList[k].id) {
            arrName.push(arrList[k]);
            break;
          }
        }
      }
    }
  }
  return arrName;
}

// 把嵌套的数据平铺-用于个人信息
export function tileNestingData(data, oData) {
  let arr = oData || [];
  if (!data || !data.length) {
    return arr;
  }

  for (var i = 0, lenI = data.length; i < lenI; i++) {
    if (data[i].children && data[i].children.length) {
      let a = data[i].children;
      data[i].children = null;
      arr.push(data[i]);
      tileNestingData(a, arr);
    } else {
      arr.push(data[i]);
    }
  }
  return arr;
}

export function inArrayData(data, value, key) {
  let switchBtn = false;
  let k = key || "id";
  if (!data || !data.length) {
    return switchBtn;
  }
  data.forEach(item => {
    if (item[k] == value) {
      switchBtn = true;
      return;
    }
  });
  return switchBtn;
}

export function getDataInArray(data, value, key) {
  let switchBtn = false;
  let k = key || "id";
  if (!data || !data.length) {
    return switchBtn;
  }
  data.forEach(item => {
    if (item[k] == value) {
      switchBtn = item;
      return;
    }
  });
  return switchBtn;
}
// 获取几天前或几天后的时间
export function getGivenDate(day) {
  var date1 = new Date();
  var date2 = new Date(date1);
  date2.setDate(date1.getDate() + day);
  var time2 =
    date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
  return time2;
}

// 获取倒计时时间-参数为剩余的时间戳
export function getCountdown(surplusOfTimestamp, isFull) {
  let leftd = Math.floor(surplusOfTimestamp / (1000 * 60 * 60 * 24)); //计算天数
  let lefth = Math.floor((surplusOfTimestamp / (1000 * 60 * 60)) % 24); //计算小时数
  let leftm = Math.floor((surplusOfTimestamp / (1000 * 60)) % 60); //计算分钟数
  let lefts = Math.floor((surplusOfTimestamp / 1000) % 60); //计算秒数

  leftd = leftd < 10 ? "0" + leftd : leftd;
  lefth = lefth < 10 ? "0" + lefth : lefth;
  leftm = leftm < 10 ? "0" + leftm : leftm;
  lefts = lefts < 10 ? "0" + lefts : lefts;
  let str = lefth + ":" + leftm + ":" + lefts;
  str = isFull ? leftd + "天" + str : str;
  return str; //返回倒计时的字符串
}
